package com.easybuilder.common.handler;

import com.easybuilder.common.base.controller.BaseController;
import com.easybuilder.common.exception.BaseException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

/**
 * @author ZXS
 * @version 1.0
 * @description 类
 * @date 2023-5-5 10:55
 */
@ControllerAdvice
@ResponseBody
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(BaseException.class)
    public BaseController.Result<?> baseExceptionHandler(HttpServletResponse response, BaseException ex){
        log.info("BaseException异常拦截,异常编码={},异常描述={}",ex.getCode(), ex.getMessage());
        ex.printStackTrace();
        return new BaseController.Result<>(ex.getCode().value(),false,null, ex.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public BaseController.Result<?> exceptionHandler(HttpServletResponse response, Exception ex){
        log.info("Exception异常拦截,异常描述={}", ex.getMessage());
        ex.printStackTrace();
        return new BaseController.Result<>(HttpStatus.INTERNAL_SERVER_ERROR.value(), false, null, ex.getMessage());
    }
}
